package com.woniu.service;

import com.woniu.entity.*;
import com.woniu.mapper.CheckStatusMapper;
import com.woniu.mapper.CheckinfoMapper;
import com.woniu.mapper.StudentsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.*;

@Service
public class CheckinfoService {

    @Autowired
    private StudentsMapper studentsMapper;

    @Autowired
    private CheckinfoMapper checkinfoMapper;

    @Autowired
    private CheckStatusMapper checkStatusMapper;



    public List<Checkinfo> findAllStudentsCheckInfo(){
        List<Checkinfo> result = new ArrayList<>();

        //把学生信息查出来放在列表里
        List<Students> studentsList = studentsMapper.selectAll();

        for (Students s : studentsList) {
            Checkinfo checkinfo = new Checkinfo();//创建一个考勤对象
            checkinfo.setStuName(s.getStuName());//学生姓名
            checkinfo.setStuID(s.getStuID());//学生学号
            checkinfo.setStuClass(s.getStuClass());//学生班级
            checkinfo.setCheckDate(new Date());

            result.add(checkinfo);
        }
        return result;
    }

    public void saveStudentsCheckInfo(List<Checkinfo> list) {

        for (Checkinfo checkinfo : list) {
            checkinfoMapper.insert(checkinfo);
        }
    }

    public List<CheckinfoDetails> findAllStudentsCheckInfoDetails(String stuClass,
                                                                  String stuName,
                                                                  Integer month,
                                                                  Integer pageNum) {


        List<CheckinfoDetails> list = checkinfoMapper.findAllStudentsCheckInfoDetails(stuClass, stuName, month, pageNum);


        return list;
    }


    public List<CheckinSummary> selectCheckinfoSummary(String stuClass){




        Map<String,Integer> hours = new HashMap();
        Map<String,Integer> score = new HashMap();

        List<CheckStatus> statusList = checkStatusMapper.selectAll();
        for (CheckStatus checkStatus : statusList) {
            hours.put(checkStatus.getCheckStatus(),checkStatus.getHours());
            score.put(checkStatus.getCheckStatus(),checkStatus.getScore());
        }

        List<CheckinSummary> list = checkinfoMapper.selectCheckinfoSummary(stuClass);
        for (CheckinSummary checkinSummary : list) {
            int dutycounts = 0;//应出勤次数
            //计算应该出勤小时数
            if(dutycounts<checkinSummary.getDutycnt()){
                dutycounts = checkinSummary.getDutycnt();
                checkinSummary.setDutyHoursSummary(dutycounts*9);//应出勤是当前表中当前班级按学生分组记录数最多的数量*9
            }

            //
            checkinSummary.setAbsenceHoursSummary(
                    checkinSummary.getDayLeaveCount()*hours.get("全天请假")+
                    checkinSummary.getAfterLeaveCount()*hours.get("下午请假")+
                    checkinSummary.getAfterSkipCount()*hours.get("下午旷到")+
                    checkinSummary.getDaySkipCount()*hours.get("全天旷到")+
                    checkinSummary.getEveningLeaveCount()*hours.get("晚自习请假")+
                    checkinSummary.getLateCount()*hours.get("迟到")+
                    checkinSummary.getLeaveEarlyCount()*hours.get("晚自习早退")+
                    checkinSummary.getMorningLeaveCount()*hours.get("上午请假")+
                    checkinSummary.getMorningSkipCount()*hours.get("上午旷到"));



            checkinSummary.setScores(
                    checkinSummary.getDutyHoursSummary()*9+
                    checkinSummary.getDayLeaveCount()*score.get("全天请假")+
                    checkinSummary.getAfterLeaveCount()*score.get("下午请假")+
                    checkinSummary.getAfterSkipCount()*score.get("下午旷到")+
                    checkinSummary.getDaySkipCount()*score.get("全天旷到")+
                    checkinSummary.getEveningLeaveCount()*score.get("晚自习请假")+
                    checkinSummary.getLateCount()*score.get("迟到")+
                    checkinSummary.getLeaveEarlyCount()*score.get("晚自习早退")+
                    checkinSummary.getMorningLeaveCount()*score.get("上午请假")+
                    checkinSummary.getMorningSkipCount()*score.get("上午旷到"));

            DecimalFormat df = new DecimalFormat("#00.0");
//            String formattedNum = df.format(num);
            //计算出勤率
            checkinSummary.setDutyRatio(
                    df.format((checkinSummary.getDutyHoursSummary()+checkinSummary.getAbsenceHoursSummary())*1.0/checkinSummary.getDutyHoursSummary()*100)+"%"
            );
            checkinSummary.setAbsenceHoursSummary(-1*checkinSummary.getAbsenceHoursSummary());//转成正数显示
        }


        return list;

    }
}
